package com.ychen.goods;

import com.ychen.commons.entity.goods.ClassifyEntity;
import com.ychen.commons.entity.goods.GoodsEntity;
import com.ychen.commons.utils.Utils;
import com.ychen.goods.dao.ClassifyDao;
import com.ychen.goods.dao.GoodsDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class InitGoods {

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private ClassifyDao classifyDao;

    @Autowired
    protected MongoTemplate mongoTemplate;

    @Test
    public void initClassify() {

    }


        @Test
    public void initData() {
        ClassifyEntity classifyEntity = new ClassifyEntity();
        classifyEntity.setClassifyId(Utils.getSnowflakeId());
        classifyEntity.setClassifyName("书籍");
        classifyEntity.setClassifyParentId(0L);
        classifyDao.save(classifyEntity);

        GoodsEntity goodsEntity = new GoodsEntity();


        String[] languages = {"java", "JavaScript", "PHP", "c++", "python", "mongodb", "mysql"};
        String[] deeps = {"入门", "进阶", "深入", "精通"};

        goodsEntity.setClassifyId(classifyEntity.getClassifyId());
        goodsEntity.setSellerId(1039544854675247104L);

        for (int i = 0; i < 100; i++) {
            int salesVol = (int) (Math.random() * 500);
            int commentNum = (int) (Math.random() * salesVol);

            goodsEntity.setGoodsName(languages[(int) (Math.random() * languages.length)] + deeps[(int) (Math.random() * deeps.length)] + i);
            goodsEntity.setDescription("本书籍描述了----" + i);
            goodsEntity.setGoodsId(Utils.getSnowflakeId());

            goodsEntity.setSupSalesVolume(salesVol);
            goodsEntity.setCommentNum(commentNum);
            mongoTemplate.insert(goodsEntity);
            System.out.println("success--" + i);
        }

    }

    @Test
    public void getRecommendGoods() {
        Query query = new Query();
        query.with(new Sort(Sort.Direction.DESC, "SUPSalesVolume"));
        query.skip(10);
        query.limit(10);

//        classifyDao.

        List<GoodsEntity> result = mongoTemplate.find(query, GoodsEntity.class, "goodsEntity");
//        mongoTemplate.aggregate();
        for (int i = 0; i < result.size(); i++) {
            System.out.println(result.get(i).toString());
        }

    }


}
